<?php
session_start();
require_once 'classes/User.class.php';
require_once 'classes/Pizzeria.class.php';
function getAction(){
	if(isset($_POST['action'])){
		return $_POST['action'];
	}
}
function transform($xml_doc, $xsl) {
		$xp = new XsltProcessor();
		$xp->importStylesheet($xsl);
		if ($html = $xp->transformToXML($xml_doc)) {
		echo $html;
		} else {
			trigger_error('XSL transformation failed.', E_USER_ERROR);
		}
}
function register(){
	if(isset($_POST['username'])&&isset($_POST['password'])){
		$user=User::register($_POST['username'],$_POST['password']);
		if($user!=null){
			echo "ok";
		}
		else{
			echo "nok";
		}
	}
	else{
		echo "nok";
	}
}
function setCommandes(){
	$_SESSION['commande']['pizzas'][]=$_POST['pizzaid'];
	$dom=new DOMDocument();
	$dom->appendChild($dom->importNode(Pizzeria::getCommandesXML(),true));
	$xsl=new DOMDocument();
	$xsl->load('commande.xsl');
	echo transform($dom, $xsl);
}
function modifPizza($pizzaid){
	Pizzeria::$tableau_pizzas=Pizza::getAll();
	$xp=new XSLTProcessor();
	$xml= new DOMDocument('1.0','utf-8');
	$xml->formatOutput = true;
	$xml->appendChild($xml->importNode(Pizzeria::getPizzasXML(),true));
	$xsl=new DOMDocument();
	$xsl->load('modifPizzas.xsl');
	$xp->importStylesheet($xsl);
	$xp->setParameter('', 'modifpizza',$pizzaid);
	if ($html = $xp->transformToXML($xml)) {
		echo $html;
	} else {
		trigger_error('XSL transformation failed.', E_USER_ERROR);
	}	
}
function login(){
	if(isset($_POST['login'])&&isset($_POST['password'])){
		$user=User::auth($_POST['login'], $_POST['password']);
		if($user!=null){
			$_SESSION['user']=$user->getEmail();
			if($user->isadmin($user->getEmail())){
				$_SESSION['isadmin']='yes';
			}
			else{
				$_SESSION['isadmin']='no';
			}
			echo "ok";
		}
		else{
			echo "nok";
		}
	}	
}
function ajaxProcessing(){
	$isadmin=false;
	if(isset($_SESSION['isadmin'])){
		$isadmin=($_SESSION['isadmin']=='yes');
	}
	switch (getAction()){
		//login 
		case 'login' :
			login();
			break;
		//enregistrement
		case 'register' :
			register();
			break;
		case 'commander-pizza' :
			if(isset($_POST['pizzaid'])){
				setCommandes();
			}
			break;
		case 'logout' :
			session_destroy();
			break;
		case 'modifier-pizza' :
			if(isset($_POST['pizzaid'])&&$isadmin){
				echo modifPizza($_POST['pizzaid']);
			}
			break;
		case 'valider-modif-pizza' :
			if($isadmin&&isset($_POST['pizzaid'])&&isset($_POST['base'])&&isset($_POST['prix'])&&isset($_POST['nom'])&&isset($_POST['ingredients'])){
				$pizza=new Pizza($_POST['pizzaid'], $_POST['nom'], $_POST['prix'], $_POST['base']);
				$pizza->clearIngredients();
				$count=count($_POST['ingredients']);
				for($i=0;$i<$count;$i++){
					$pizza->addIngredient($_POST['ingredients'][$i]);
				}
				$pizza->save();
				echo 'ok';
			}
			break;
		case 'valider-commande' :
			Pizzeria::getCommande();
			Pizzeria::$commande->save();
			$_SESSION['commande']='';
			echo 'ok';
			break;
		case 'annuler-order' :
			if($isadmin&&isset($_POST['orderid'])){
				Order::cleanup($_POST['orderid']);
			}
			break;
		case 'annuler-order-client' :
				$_SESSION['commande']='';
			break;
		case 'changement-order-status' :
			if($isadmin&&isset($_POST['orderid'])){
				$order=Order::getById($_POST['orderid']);
				$order->nextStatus();
			}
			break;
	}
}
ajaxProcessing();